package com.developer.platform.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.developer.platform.domain.Menu;
import com.developer.platform.domain.repository.IMenuRepository;
import com.developer.platform.infrastructure.data.mapper.MenuMapper;
import com.developer.platform.infrastructure.enums.MenuEnum;
import com.developer.platform.infrastructure.enums.StatusEnum;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Repository;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Repository
public class MenuRepository implements IMenuRepository {

    private final MenuMapper menuMapper;

    public MenuRepository(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public List<Menu> findByUserId(long userId) {
        return menuMapper.findByUserId(userId);
    }

    @Override
    public List<Menu> findByStatusType(StatusEnum status, MenuEnum type) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(status), Menu::getStatus, status);
        lambdaQueryWrapper.eq(Objects.nonNull(type), Menu::getType, type);
        return menuMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public int parentCount(long id) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getParentId,id);
        return menuMapper.selectCount(lambdaQueryWrapper);
    }

    @Override
    public void save(Menu menu) {
        menuMapper.insert(menu);
    }

    @Override
    public void delete(long id) {
        menuMapper.deleteById(id);
    }

    @Override
    public void updateById(Menu menu) {
        menuMapper.updateById(menu);
    }
}
